﻿using System;
using System.Text.RegularExpressions;

namespace DreamCube.Foundation.Validate
{
    public static class MyValidate
    {
        /// <summary>
        /// 判断是否整型采用正则表达式的方式
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static Boolean IsInteger(String input)
        {
            return Validate("IsInteger", input);
        }

        /// <summary>
        /// 判断是否整型，采用尝试转换的方式
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static Boolean IsIntegerEx(String input)
        {
            Int32 result = 0;
            return Int32.TryParse(input, out result);
        }

        /// <summary>
        /// 判断输入的字符串是否只包含数字和英文字母
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static Boolean IsNumAndEnCh(String input)
        {
            return Validate("IsNumAndEnCh", input);
        }

        /// <summary>
        /// 判断是输入是否合法的Email格式
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static Boolean IsEmail(String input)
        {
            return Validate("IsEmail", input);
        }

        /// <summary>
        /// 判断是否包含中文字符
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static Boolean IsContainChs(String input)
        {
            return Validate("IsContainChs", input);
        }

        /// <summary>
        /// 判断是否合法的url地址
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static Boolean IsUrl(String input)
        {
            return Validate("IsUrl", input);
        }

        /// <summary>
        /// 调用验证方法
        /// </summary>
        /// <param name="input"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static Boolean Validate(String key, String input)
        {
            Regex reg = new Regex(ValidateRuleHelper.GetRegexPatternByKey(key));
            return reg.IsMatch(input);
        }
    }
}
